
/*
  ____     ____               _______    __   __  _    _                   
 |  _ \   / __ \      /\     |__   __|   \ \ / / | |  | |                  
 | |_) | | |  | |    /  \       | |       \ V /  | |  | |                  
 |  _ <  | |  | |   / /\ \      | |        > <   | |  | |                  
 | |_) | | |__| |  / ____ \     | |       / . \  | |__| |                  
 |____/   \____/  /_/    \_\    |_|      /_/ \_\  \____/                   
   _____    ____    ______   _______     ______   _    _    _____    _____ 
  / ____|  / __ \  |  ____| |__   __|   |  ____| | |  | |  / ____|  / ____|
 | (___   | |  | | | |__       | |      | |__    | |__| | | (___   | (___  
  \___ \  | |  | | |  __|      | |      |  __|   |  __  |  \___ \   \___ \ 
  ____) | | |__| | | |         | |      | |      | |  | |  ____) |  ____) |
 |_____/   \____/  |_|         |_|      |_|      |_|  |_| |_____/  |_____/ 
                                                                           
 */ 

/*
 *  Description: BOAT XU SOFT FHSS
 *  Version: V1.00
 *  Author: boat xu
 *  Email: xifengzui@outlook.com
 *  Date: 2020-07-10 07:54:10
 *  LastEditTime: 2020-07-22 14:16:51
 */ 
/*
 *  This is a software defined FHSS,i wrote it for some cheap chip ,such as lt8920.
 *  I hav't test it on other hardware,if u do so, please let me know.
 *  this project is aim to make a common protocol to fit all hardware, not base on a special hardware.
 *  the the cost is bit rate can not boost into a best situation.
 *  
 *  NOTICE:
 *      1.No TX RX conflict detection.
 *      2.No Device match function.
 *      3.No Repeated Packet Check,but we have index for you to check if this pack is repeated or not.
 *  FEATURE:
 *      1.auto freq band weighted base on environment noise.
 *      2.change the freq table every second.
 *      3.connect lost reonnect.
 */ 
#ifndef __bsf_H__
#define __bsf_H__
#include "bsf_types.h"

/**
 *  Description: define the device is master or slaver.
 *  usually the master do the transmitter job.
 *  cant be master and slaver at same time.
 */
#define BSF_MASTER 1
#define BSF_SLAVER 0
#define DPRINT 1


#define SEND_RETRY_CNT 3 //if cnt =0 retry forever 
/**
 *  Description: one jump count(by your define of timer), slaver is always slowr than master
 */
#define FHSS_MASTER_TIMER_CNT 800//8ms
#define FHSS_MASTER_ABNORMAL_TIMER_CNT (3*FHSS_MASTER_TIMER_CNT)
#define FHSS_MASTER_ACK_TIMEOUT_CNT 5// 0.5ms
#define FHSS_MASTER_TABTIMEOUT_CNT 60000 //600ms

#define FHSS_SLAVER_TIMER_CNT 810
#define FHSS_SLAVER_ABNORMAL_TIMER_CNT (3*FHSS_SLAVER_TIMER_CNT)
#define FHSS_SLAVER_TABTIMEOUT_CNT 60000 //600ms

/**
 *  Description: framework will use this function to know is hardware come to a new packet
 *  Param: 
 *  Return: 
 */
extern bsf_IsPktArrival_Func IsPktArrival;
/**
 *  Description: framework will use this function in anytime
 *  Param: 
 *  Return: 
 */
extern bsf_SendPkt_Func  SendPkt;

/**
 *  Description: framework will queue the sendpkt to know the send status.
 *  Param: 
 *  Return: ref to b_packSendStat
 */
extern bsf_SendingStatus_Func SendingPktStatus;

/**
 *  Description: framework will use this function in anytime,
 *  Param: the input thing is buffer.
 *  Return: if there is no data,please return 0,else return the length of the data.
 */
extern bsf_GetPkt_Func  GetPkt;

/**
 *  Description: frame will set the hardware freq in any time, please make sure this function work.
 *  Param: 0~128 input 
 *  Return: 
 */
extern bsf_SetFreq_Func SetFreq;

/**
 *  Description: let the framework to know the current timestamp. return uint16 value
 *  the timestamp must generate every 0.1ms
 *  Param: 
 *  Return: 
 */
extern  bsf_GetCurrentTimestamp_Func GetCurrentTimestamp;

/**
 *  Description: onConnect Create,the bsf will fire this event to let the user know.
 *  Param: 
 *  Return: 
 */
extern bsf_OnConnOkEvent OnConnOkEvent;


/**
 *  Description: onConnect lost,the bsf will fire this event to let the user know.
 *  Param: 
 *  Return: 
 */
extern bsf_OnConnLostEvent OnConnLostEvent;

/**
 *  Description: if u care about the send result , just fill this fucntion
 *  Param: 
 *  Return: 
 */
extern bsf_PktSendResultEvent PktSendResultEvent;

void bsf_init(void);

/**
 *  Description: please put it into loop thread,make the time as short as possible,
 *  bit rate speed depend on this,you can try it by yourself by simplely change the loop rate.
 *  I hav't try to put it into a timer interrupt to run, you can have a try.
 *  Param: 
 *  Return: 
 */
void bsf_do(void);



/**
 *  Description: in order to boost the write efficiency.
 *  you can simplely use this to get the direct write buffer address.direct operat the memory.
 *  please be sure Begin And End Pair, if not , it might cause unknow bug.
 *  Param: index: your define of packet index, your data need to be send ,if putLen == 0 ,the data will be ignore. buffer will keep free
 *  Return: 
 */
b_boolean bsf_sendPkt_Begin(b_u8_t **outBuf);
void bsf_sendPkt_End(b_u16_t index,b_u8_t putLen);

/**
 *  Description: is allow packet drop,some times the system might need to send packet as fast as possible,so will allow some loss packet.
 *  Param: default system will allow pkt drop.
 *  Return: 
 */
void bsf_sendPkt_isAllowPktDrop(b_boolean allowFlag);

/**
 *  Description:  in order to boost the read efficiency.
 *  you can simplely use this to get the direct read buffer address.direct operat the memory.
 *  please be sure Begin And End Pair, if not , it might cause unknow bug.and 
 *  Param:  outIndex: the index that user put in ,
 *          outBuf: the addres of the buffer.
 *          isGetFinished: the flag that indecate the buffer,is not for me, will keep stay in buffer,until next one read it.
 *  Return: 
 */
b_boolean bsf_readPkt_Begin(unsigned short int *outIndex,unsigned char **outBuf);

void bsf_readPkt_End(b_boolean isGetFinished);



#endif